/**
 * This class stores football scores
 */
public class FootballScores
{
	protected final String[] teams; //not modified after init
	protected Point[][] scores;

	/**
	 * Initializes teams from the parameter and scores to null
	 */
	public FootballScores(String[] teams)
	{
		this.scores = new Point[teams.length][teams.length];
		this.teams = teams; //keep a reference to the given object
	}

	/**
	 * Debugging function
	 * @return displayable string
	 */
	public String toString() //like Python's __str__() method
	{
		String res = "";
		for (int i=0; i<teams.length; i++)
		{
			res += "Team "+i+" <"+teams[i]+">:\t";
			for (int j=0; j<teams.length; j++)
				if (j != i) //don't display useless cells (always empty...)
					res += j+" <"+teams[j]+"> "+scores[i][j]+"\t";
			res += "\n"; //next line
		}
		return res;
	}

	/**
	 * Finds a team's index by name
	 * @param name Team to look for
	 * @return the team's index, -1 if not found
	 */
	protected int findTeam(String name)
	{
		for (int i=0; i<teams.length; i++)
			//don't EVER use == on Strings (unless you wish to compare instances)
			if (teams[i].equals(name)) return i;
		return -1;
	}

	/**
	 * Sets the score between two teams
	 * @param i1 First team
	 * @param i2 Second team
	 * @param p Score
	 * @return true on error, otherwise false
	 */
	protected boolean setScore(int i1, int i2, Point p)
	{
		Point sym = new Point(p.getY(), p.getX()); //reversed

		if (i1==i2 || i1==-1 || i2==-1) return true; //logical OR
		scores[i1][i2] = p; //given score
		scores[i2][i1] = sym; //symmetric score
		
		return false;
	}

	/**
	 * Sets the score between two teams by name
	 * @param e1 First team
	 * @param e2 Second team
	 * @param p Score
	 * @return true on error, otherwise false
	 */
	public boolean setScore(String e1, String e2, Point p)
	{
		int i1=findTeam(e1), i2=findTeam(e2);
		return setScore(i1, i2, p);
	}

	/**
	 * Simulates a match
	 * @return random score (maximum 5)
	 */
	protected Point simulateMatch()
	{
		int s1=(int)(Math.random()*5), s2=(int)(Math.random()*5);
		return new Point(s1, s2);
	}

	/**
	 * Simulates a (random) match between teams e1 and e2 (by name).
	 * @param e1 First team
	 * @param e2 Second team
	 * @return true on error, otherwise false
	 */
	public boolean simulateMatch(String e1, String e2)
	{
		return setScore(e1, e2, simulateMatch());
	}

	/**
	 * Simulates remaining matches
	 */
	public void simulateChampionship()
	{
		for (int i=0; i<teams.length; i++)
			for (int j=0; j<teams.length; j++)
				if (i!=j && scores[i][j]==null)
					setScore(i, j, simulateMatch());
	}
}
